const { $Toast } = require('../../dist/base/index');
const server=require("../../utils/server.js");
var app=getApp();
Page({
  data: {
    phone: '',
    password: '',
    lng:'',
    lat:'',
    visible1: false,
    actions1: [
      {
        name: '绑定新账号',

      },
       {
        name: '绑定已有账号',
      }

    ],
  },
 onLoad:function(){
   this.setCode();
   let id = wx.getStorageSync("uid");
   if (id) {
      
     wx.switchTab({
       url: '../index/index',
     })
   } else {
  
   }
 },
  goRegist() {
    wx.navigateTo({
      url: '../regist/regist',
    })
  },
  setCode(){
    let that=this;
     wx.login({
      success:function(res){
        
       wx.setStorageSync('code', res.code);
      
      }
    })
   
  },
  onGotUserInfo(res){
    
    wx.setStorageSync('info', res.detail.rawData)
  },
  handler({detail}){
    
      wx.navigateTo({
        url: '../bindWx/bindWx?type='+detail.index,
      })
    
  },
  vertifyWechat(){
    let that = this;
    let code = wx.getStorageSync('code');
    wx.getStorage({
      key: 'code',
      success: function(res) {
        var data=res.data;
        server.postReq('/model/wxPay/api/wx/openId',{
          code:data
        },function(res){
          
          if (res.statusCode==200){
            if(res.data.openid){
            
              wx.setStorageSync('openid', res.data.openid);
              that.isBind(res.data.openid);
            }else{
              that.handleText("发了错误，请尝试重新进入");
            }
            
          }
          
        })
        
      },
    })
   
  

  },
  isBind(openid){
    let that=this;
    server.postReq('/model/wx/XCXwxLogin', {
      openId: openid
    },function (re) {
         if (re.data.Isbinding==0) {
          
          that.setData({
            visible1: true
          });
        }
        else{
           
          wx.setStorageSync('imgUrl', re.data.headPortraitLink)
          wx.setStorageSync('uid', re.data.data.id);
          wx.switchTab({
             url: '../index/index',
          });
        };

       
    })
  },
  goForgot() {
    wx.navigateTo({
      url: '../forgetPwd/forgetPwd',
    })
  },
  // 获取输入账号 
  phoneInput: function (e) {
    this.setData({
      phone: e.detail.value
    })
  },

  // 获取输入密码 
  passwordInput: function (e) {
    this.setData({
      password: e.detail.value
    })
  },
 

 sendLocation(id,lng,lat){
   var that = this;
   server.postReq('/mobile/updataUserSite',{
     userId:id,
     siteLongitude:lng,
     siteLatitude:lat
   },(res)=>{
      if(res.data.code==1){

      }else{
        //this.handleText("获取位置失败");
      }
   })
 },
 getLocation(){
   let that=this;
   let flag=new Object();
   wx.getLocation({
     type: 'wgs84',
     success: function (res) {
       var lng=res.longitude;
       var lat=res.latitude;
       wx.setStorageSync('lng', lng);
       wx.setStorageSync('lat', lat);
      //  that.setData({
      //    lng:lng,
      //    lat:lat
      //  })
     }
   
   });
  
 },
  // 登录 
  login: function () {
    var self=this;
     var url = app.globalData.preUrl + "/mobile/Login";
        if (this.data.phone.length == 0 || this.data.password.length == 0) {
          this.handleText("请填写完整");
        } else {
         server.postReq("/mobile/Login",{
            "loginName": self.data.phone,
            "passWord": self.data.password
          },function(res){
            wx.getStorage({
              key: 'code',
              success: function (res) {
                var data = res.data;
                server.postReq('/model/wxPay/api/wx/openId', {
                  code: data
                }, function (res) {

                  if (res.statusCode == 200) {
                    if (res.data.openid) {

                      wx.setStorageSync('openid', res.data.openid);
                      
                    } else {
                      wx.redirectTo({
                        url: '../login/login',
                      })
                    }

                  }

                })

              },
            })
            self.handleText(res.data.msg);
            if (res.data.code == 1) {
            // self.getLocation();
              
             // let lng = self.data.lng;
              //let lat = self.data.lat;
            //  self.sendLocation(res.data.data.id, lng, lat)
              wx.setStorageSync('imgUrl', res.data.headPortraitLink)
              wx.setStorage({
                key: 'uid',
                data: res.data.data.id,
                success: function (res) {
                    setTimeout(function(){
                    wx.switchTab({
                      url: '../index/index',
                    })
                  },1000)

                }
              })
            }
          })
        
        } 

     
  
  },
  handleCancel1() {
    this.setData({
      visible1: false
    })
  },
  handleText(msg) {
    $Toast({
      content:msg
    });
  },
  handleSuccess(successMsg) {
    $Toast({
      content:successMsg,
      type: 'success'
    });
  },
  handleWarning() {
    $Toast({
      content: '警告的提示',
      type: 'warning'
    });
  },
  handleError() {
    $Toast({
      content: '错误的提示',
      type: 'error'
    });
  },
  handleLoading() {
    $Toast({
      content: '加载中',
      type: 'loading'
    });
  },
}) 